package com.jintoufs.util.weixin;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jintoufs.util.http.HttpRequestUtils;
import org.cosmos.modules.web.msg.AjaxMsg;

/**
 * crateTime:2017/5/11 10:52
 * author:YangHao
 * description:消息推送
 */
public class WeixinMessagePush {
    private String appId;
    private String secret;

    public AjaxMsg sendMessage(WeixinMessage weixinMessage, String accessToken) {
        AjaxMsg ajaxMsg = new AjaxMsg();
        String url = "https://api.com.jintoufs.service.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken;

        String param = JSON.toJSONString(weixinMessage);
        System.out.println("参数：" + param);
        String result = HttpRequestUtils.httpPost(url, param);
        JSONObject resultObj = JSONObject.parseObject(result);
        if (resultObj.get("errcode").equals("0")) {
            ajaxMsg.setCode(AjaxMsg.SUCCESS);
            System.out.println("SUCCESS");
        } else {
            System.out.println("FAIL：" + result);
            ajaxMsg.setCode(AjaxMsg.FAILURE);
            ajaxMsg.setMessage("消息推送失败");
        }
        return ajaxMsg;
    }

    public void setAppId(String appId) {
        this.appId = appId;
    }

    public String getAppId() {
        return appId;
    }

    public void setSecret(String secret) {
        this.secret = secret;
    }

    public String getSecret() {
        return secret;
    }
}
